//
//  SSBasicPopView.m
//  SesameSister
//
//  Created by Mirko_wei on 15/5/9.
//  Copyright (c) 2015年 Sesame. All rights reserved.
//

#import "MCBasicPopView.h"
@implementation MCBasicPopView

+(instancetype) popView:(BOOL) touchOutSideHide
{
    MCBasicPopView *view = (MCBasicPopView *)[self loadIBView];
    view.touchOutSideHide = touchOutSideHide;
    if (view) {
        return view;
    }
    return [[MCBasicPopView alloc] init];
}

-(void) showPopViewInKeyWindow
{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    [self showPopViewInView:view];
}

-(void) showPopViewInView:(UIView *) view
{
//    if (self.superview) {
        self.center = CGPointMake(view.frame.size.width/2, view.frame.size.height/2);
        [self setMaskView:view];
        [view addSubview:self];
//    }
    CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    bounceAnimation.fillMode = kCAFillModeBoth;
    bounceAnimation.removedOnCompletion = YES;
    bounceAnimation.duration = 0.4;
    bounceAnimation.values = @[
                               [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2f, 1.2f, 1.2f)],
                               [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    bounceAnimation.keyTimes = @[@0.0f, @0.6f, @1.0f];
    bounceAnimation.timingFunctions = @[
                                        
                                        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    
    [self.layer addAnimation:bounceAnimation forKey:@"transform"];
}

-(void) setMaskView:(UIView *) view
{
    self.maskBackgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    self.maskBackgroundView.userInteractionEnabled = YES;
    self.maskBackgroundView.backgroundColor = [UIColor blackColor];
    self.maskBackgroundView.alpha = 0;
    UITapGestureRecognizer *hideGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hanldeTapEvent:)];
    [self.maskBackgroundView addGestureRecognizer:hideGesture];
    [view addSubview:self.maskBackgroundView];
    [UIView animateWithDuration:0.4 animations:^{
        self.maskBackgroundView.alpha = 0.5;
    }];
}

-(void) hanldeTapEvent:(UITapGestureRecognizer *) getsture
{
    if (!self.touchOutSideHide) {
        return;
    }
    CGPoint touchPoint = [getsture locationInView:self.maskBackgroundView];
    
    if (!CGRectContainsPoint(self.frame, touchPoint)) {
        [self hidePopView:YES];
    }
}

-(void) hidePopView:(BOOL) anima
{
    void (^completion)(void) = ^{
        [self.maskBackgroundView removeFromSuperview];
        [self removeFromSuperview];
    };
    if (anima) {
        [UIView animateWithDuration:0.3 animations:^{
            self.maskBackgroundView.alpha = 0.0f;
            self.alpha = 0.0f;
            self.transform = CGAffineTransformMakeScale(0.9, 0.9);
        }completion:^(BOOL finished) {
            completion();
        }];
    }else{
        completion();
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
